const KoaRouter = require('koa-router');
const { koaBody } = require('koa-body');
const router = new KoaRouter({ prefix: '/user' });
const controller = require('../controllers/userController');
const { authenticateToken, addUser } = require('../middleware/validation');

const koaBodyFormidable = koaBody({
  multipart: true, // 开启对数据的解析
  formidable: { // 上传文件
    uploadDir: './public/avatar', // 文件上传后存放的目录
    keepExtensions: true, // 保持后缀名
  }
});

router.get('/list', authenticateToken, controller.getUsers);
router.post('/add', authenticateToken, addUser, controller.addUser);
router.put('/update/:id', authenticateToken, controller.updateUser);
router.delete('/delete/:id', authenticateToken, controller.deleteUser);
router.post('/editAvatar', koaBodyFormidable, controller.editAvatar);

module.exports = router;